package com.wsd.serviceImpl;

import com.wsd.common.Result;
import com.wsd.constant.ResultCode;
import com.wsd.dao.RepaymentPlanMapper;
import com.wsd.domain.RepaymentPlan;
import com.wsd.service.OverdueUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

@Service
public class OverdueUpdateServiceImpl implements OverdueUpdateService {

    private BigDecimal overdueInterestRateOfDay = new BigDecimal(0.08*(1+0.3)/360); //逾期日利率

    @Autowired
    RepaymentPlanMapper repaymentPlanMapper;



    @Override
    public Result UpdateOverdueInformation() {




        //初始化List，用来记录逾期天数为60天的还款计划
        List<RepaymentPlan> overdueSixtyDaysRepaymentPlans = new LinkedList<>();


        //更新已有逾期还款计划
        List<RepaymentPlan> overdueRepaymentPlans = repaymentPlanMapper.findAllOverduePlans();
        for (RepaymentPlan repaymentPlan : overdueRepaymentPlans)
        {

            LocalDate repaymentDate = repaymentPlan.getRepaymentTime();
            Long overdueDayCal = LocalDate.now().toEpochDay() - repaymentDate.toEpochDay();
            Long overdueDayTable = repaymentPlan.getOverdueDay().longValue();


            //判断实际逾期天数与表中记录是否已经同步，防止多次调用服务导致逾期天数持续增加
            if(overdueDayCal == overdueDayTable)
            {
                System.out.println(LocalDateTime.now()+"table had been updated!");
                continue;
            }

            //记录逾期天数为60天的还款计划
            if(repaymentPlan.getOverdueDay() == 60)
            {
                overdueSixtyDaysRepaymentPlans.add(repaymentPlan);
            }
            //更新逾期天数
            repaymentPlan.setOverdueDay(repaymentPlan.getOverdueDay()+1);
            //更新逾期利息
            BigDecimal newOverdueInterest = repaymentPlan.getRepayableAmount().multiply(overdueInterestRateOfDay);
            repaymentPlan.setOverdueInterest(repaymentPlan.getOverdueInterest().add(newOverdueInterest));

            //更新总利息
            repaymentPlan.setInterestPart(repaymentPlan.getInterestPart().add(newOverdueInterest));

            //更新可还金额
            repaymentPlan.setRepayableAmount(repaymentPlan.getRepayableAmount().add(newOverdueInterest));


            //更新
            repaymentPlanMapper.updateByPrimaryKeySelective(repaymentPlan);

        }

        //处理新逾期还款计划
        List<RepaymentPlan> newOverdueRepaymentPlans = repaymentPlanMapper.findNewOverduePlans();
        for (RepaymentPlan repaymentPlan : newOverdueRepaymentPlans)
        {
            //修改还款计划状态为逾期
            repaymentPlan.setRepaymentStatus("2");

            //更新逾期天数为实际逾期天数
            Long overdueDayCal = LocalDate.now().toEpochDay() - repaymentPlan.getRepaymentTime().toEpochDay();
            repaymentPlan.setOverdueDay(overdueDayCal.intValue());
            //更新逾期利息
            BigDecimal newOverdueInterest = repaymentPlan.getRepayableAmount().multiply(overdueInterestRateOfDay);
            repaymentPlan.setOverdueInterest(newOverdueInterest);

            //更新总利息
            repaymentPlan.setInterestPart(repaymentPlan.getInterestPart().add(newOverdueInterest));

            //更新可还金额
            repaymentPlan.setRepayableAmount(repaymentPlan.getRepayableAmount().add(newOverdueInterest));


            //更新
            repaymentPlanMapper.updateByPrimaryKeySelective(repaymentPlan);

        }

        //针对逾期天数为60天的还款计划(至此已更新为61天)，取消后续还款计划，都合并至本期
        for(RepaymentPlan repaymentPlan : overdueSixtyDaysRepaymentPlans)
        {
            Long loanId = repaymentPlan.getLoanId();
            List<RepaymentPlan> repaymentPlans = repaymentPlanMapper.selectRepaymentPlanByLoanId(loanId);

            //对同一贷款ID的还款计划，遍历有60天逾期还款计划贷款的左右还款计划
            for(RepaymentPlan repaymentPlanInner : repaymentPlans)
            {
                //处理空值
                if(repaymentPlanInner.getOverdueDay()==null)
                {

                    repaymentPlanInner.setOverdueDay(0);
                }
                //针对已结清以及逾期天数为61天的，不做处理
                if(repaymentPlanInner.getOverdueDay() == 61 || repaymentPlanInner.getRepaymentStatus().equals("1"))
                {
                    continue;
                }

                //作废其他还款计划
                repaymentPlanInner.setRepaymentStatus("3");
                //对已出账单的应还金额，未出账单的本金部分，合并至逾期天数为61天的还款计划中

                //已出账单  --即还款日期与当前日期相差在一个月以内
                // 例如：当前日期7.26 还款日期在8.26及其之前都算已出账单 [ 7.26.plusMonths(1).plusDays(1) = 8.27]
                if(repaymentPlanInner.getRepaymentTime().isBefore(LocalDate.now().plusMonths(1).plusDays(1)))
                {
                    if(repaymentPlanInner.getOverdueInterest()==null)
                    {
                        repaymentPlanInner.setOverdueInterest(new BigDecimal(0));
                    }
                    repaymentPlan.setRepayableAmount(repaymentPlan.getRepayableAmount().add(repaymentPlanInner.getRepayableAmount()));
                    repaymentPlan.setPrincipalPart(repaymentPlan.getPrincipalPart().add(repaymentPlanInner.getPrincipalPart()));
                    repaymentPlan.setInterestPart(repaymentPlan.getInterestPart().add(repaymentPlanInner.getInterestPart()));
                    repaymentPlan.setOverdueInterest(repaymentPlan.getOverdueInterest().add(repaymentPlanInner.getOverdueInterest()));
                }

                //未出账单 把本金部分汇总至逾期61天还款计划的应还金额和本金金额
                else
                {
                    repaymentPlan.setRepayableAmount(repaymentPlan.getRepayableAmount().add(repaymentPlanInner.getPrincipalPart()));
                    repaymentPlan.setPrincipalPart(repaymentPlan.getPrincipalPart().add(repaymentPlanInner.getPrincipalPart()));

                }

                //更新逾期61天对应的后续还款计划记录
                repaymentPlanMapper.updateByPrimaryKeySelective(repaymentPlanInner);

            }


            //更新逾期61天的还款计划记录
            repaymentPlanMapper.updateByPrimaryKeySelective(repaymentPlan);

        }



        return new Result(ResultCode.SUCCESS.getCode(),"逾期信息更新成功",null);
    }
}
